package com.gxd.web.servlet;

import com.gxd.domain.Goods;
import com.gxd.domain.PageBean;
import com.gxd.service.GoodsService;
import com.gxd.service.impl.GoodsServiceImpl;
import com.gxd.util.NotNull;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@WebServlet("/goodsservlet")
@MultipartConfig(maxFileSize = 1024*1024*5,maxRequestSize = 1024*1024*20)
public class GoodsServlet extends ServletBase {
    GoodsService goodsService = new GoodsServiceImpl();
    public void bookcovers(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String picture = request.getParameter("picture");
        String basePath = this.getServletContext().getRealPath("WEB-INF/bookcovers");
        String path = basePath + File.separator+picture;
        File file = new File(path);

        FileInputStream fis = new FileInputStream(path);
        ServletOutputStream os = response.getOutputStream();
        byte [] bytes = new byte[1024*8];
        int len;
        while((len=fis.read(bytes))!=-1){
            os.write(bytes,0,len);
        }
    }


        public String addGoods(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String name = request.getParameter("name");
        int typeid = Integer.parseInt(request.getParameter("typeid"));
        String pubdate = request.getParameter("pubdate");
        double price = Double.parseDouble(request.getParameter("price"));
        int star = Integer.parseInt(request.getParameter("star"));
        String intro = request.getParameter("intro");
        Part picture = request.getPart("picture");

        String basePath = this.getServletContext().getRealPath("WEB-INF/bookcovers");
        File file = new File(basePath);
        if (!file.exists()) {
            file.mkdir();
        }

        String dis = picture.getHeader("Content-Disposition");
        String filename = dis.substring(dis.lastIndexOf("filename=") + 10, dis.length() - 1);
        filename=filename.substring(filename.lastIndexOf("\\")+1);


        String ex = filename.substring(filename.lastIndexOf(".") + 1);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newFileName=uuid+"."+ex;
        if(!newFileName.equals("")){
            picture.write(basePath+File.separator+newFileName);
            picture.delete();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Goods goods = new Goods(0,name,sdf.parse(pubdate),newFileName,price,star,intro,typeid);
        goodsService.addGoods(goods);


        return "redirect:/goodsservlet?method=getGoodsList";
    }
        public String getGoodsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int page_num=1;
        int page_size=2;
        try {
            if(!NotNull.isEmpty(pageNum)){
                page_num = Integer.parseInt(pageNum);
                if(page_num<1){
                    page_num=1;
                }
            }

            if(!NotNull.isEmpty(pageSize)){
                page_size=Integer.parseInt(pageSize);
                if(page_size<1){
                    page_size=2;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageBean pageBean = goodsService.findByPage(page_num,page_size);
        request.setAttribute("pageBean",pageBean);
        return "forward:/admin/showGoods.jsp";

    }
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        Goods goods=goodsService.findGoodById(id);

            request.setAttribute("goods",goods);
            return "forward:/goodsDetail.jsp";


    }
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response) throws Exception {

        int typeId = Integer.parseInt(request.getParameter("typeId"));
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int page_num=1;
        int page_size=2;
        if(!NotNull.isEmpty(pageNum)){
            page_num=Integer.parseInt(pageNum);
        }
        if(!NotNull.isEmpty(pageSize)){
            page_size=Integer.parseInt(pageSize);
        }
        PageBean pageBean=goodsService.findById(typeId,page_num,page_size);
        request.getSession().setAttribute("pageBean",pageBean);
        request.getSession().setAttribute("typeId",typeId);
        return "forward:/goodsList.jsp";

    }
}
